local skel = fk.CreateSkill{
  name = "kqf__luanzheng",
}

Fk:loadTranslationTable{
  ["kqf__luanzheng"] = "乱政",
  [":kqf__luanzheng"] = "准备阶段，你可以移动场上至多三张装备牌。结束阶段，所有角色可以依次移动其本回合因此失去的牌，然后每有一张牌未归原位，你摸一张牌。",

  ["#kqf__luanzheng_ask1"] = "乱政：你可以移动场上至多三张装备牌（%arg/3）",
  ["#kqf__luanzheng_ask2"] = "移动你本回合因“乱政”而失去的【%arg】",
  ["@kqf__luanzheng_movep-turn"] = "乱政",

  ["$kqf__luanzheng1"] = "本公主要做何事，还需你们说教？",
  ["$kqf__luanzheng2"] = "恩情惩刑，皆是本公主的赏赐。",
}

skel:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    --移动牌
    for _,i in ipairs({1,2,3})do
      if player.dead or #room:canMoveCardInBoard("e") == 0 then return false end
      local to = room:askToChooseToMoveCardInBoard(player, {
        prompt = "#kqf__luanzheng_ask1:::" .. i,
        skill_name = skel.name,
        cancelable = true,
        flag = "e",
      })
      if #to == 2 then
        local moveinfo = room:askToMoveCardInBoard(player, {
          target_one = to[1],
          target_two = to[2],
          skill_name = skel.name,
          flag = "e",
        })
        --添加标记
        if not moveinfo then break end
        room:setPlayerMark(moveinfo.from,"kqf__luanzheng_movep"..i.."-turn",moveinfo.card.id)--好傻逼的方法
        room:setPlayerMark(moveinfo.from,"@kqf__luanzheng_movep-turn",i)--上面用来操作，这个用来展示给玩家看
      else
        break
      end
    end
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Finish and player:usedSkillTimes(skel.name, Player.HistoryTurn) > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    --挨个移动
    for _, i in ipairs({1,2,3}) do
      for _, p in ipairs(room:getAllPlayers()) do
        if p:hasMark("kqf__luanzheng_movep"..i.."-turn") then
          if i == 1 then
            player.room:notifySkillInvoked(player, skel.name)
            player:broadcastSkillInvoke(skel.name,2)
          end
          local cid = p:getMark("kqf__luanzheng_movep"..i.."-turn")
          local excludeIds = {}
          local hasthiscard = false
          for _, pp in ipairs(room.alive_players) do
            for _, id in ipairs(pp:getCardIds("e")) do
              if id ~= cid then
                table.insert(excludeIds, id)
              else
                hasthiscard = true
              end
            end
          end
          if hasthiscard == false then
            break
          end
          local targets
          if #room:canMoveCardInBoard("e", room.alive_players, excludeIds) > 0 then
            targets = room:askToChooseToMoveCardInBoard(p, {
              prompt = "#kqf__luanzheng_ask2:::" .. Fk:getCardById(cid).name,
              skill_name = skel.name,
              cancelable = true,
              flag = "e",
              exclude_ids = excludeIds,
            })
          end
          if #targets == 2 then
            room:askToMoveCardInBoard(p, {
              target_one = targets[1],
              target_two = targets[2],
              skill_name = skel.name,
              exclude_ids = excludeIds,
              flag = "e",
            })
          end
        end
      end
    end
    --移动完了
    local numb = 0
    for _, i in ipairs({1,2,3}) do
      for _, p in ipairs(room:getAllPlayers()) do
        if p:hasMark("kqf__luanzheng_movep"..i.."-turn") then
          local cid = p:getMark("kqf__luanzheng_movep"..i.."-turn")
          numb = numb + 1--找到一个摸一张
          for _, id in ipairs(p:getCardIds("e")) do
            if id == cid then
              numb = numb - 1--但是回原位了所以不摸
            end
          end
        end
      end
    end
    if numb > 0 then
      room:drawCards(player,numb,skel.name)
    end
  end,
})

return skel